package pub.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import pub.dao.CommonDao;
import pub.model.PageBean;
@Repository
@SuppressWarnings("unchecked")
public class CommonDaoImpl extends HibernateTemplate implements CommonDao {
	public <T> List<T> CDQuery(final StringBuffer sb, final Class<T> className) throws Exception{
		Session session=super.getSessionFactory().getCurrentSession();
		Query query =session.createSQLQuery(sb.toString());
		if(null!=className){
			query.setResultTransformer(Transformers.aliasToBean(className));
		}
		List<T> list=null;
		try{
			list=query.list();
		}catch (Exception e) {
			System.out.println(e.getMessage());
			throw new Exception(e.getCause());
		}
		return list;
	}
	/**
	 * hibernateTemplate.save(t)���ر���ɹ���id
	 */
//	public <T> Object CDSave(T t) throws Exception{
//		Object id=null;
//		try {
//			id=super.save(t);
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//			throw new Exception(e.getCause());
//		}
//		return id;
//	}
	/**
	 * ������ɾ�����������
	 * @param sql
	 * @throws Exception
	 */
	public Integer CDexecut(String sql)throws Exception{ 
		Integer record=0;
		try {
			Session session =  super.getSessionFactory().getCurrentSession();  
			Query query = session.createSQLQuery(sql);  
			record=query.executeUpdate(); 
		} catch (Exception e) {
			System.out.println(e.getMessage());
			throw new Exception(e.getCause());
		}
        return record;
    }

	
	/**
	 * @author zhuxunjun
	 * @param final StringBuffer sb
	 * @param final PageBean<T> page
	 * @return PageBean
	 */
	public <T> PageBean<T> CDqueryPage(final StringBuffer sb,final PageBean<T> page,final Class<T> className) throws Exception {
		//��ѯ�������page��total
		StringBuffer total=new StringBuffer("select count(*) from (").append(sb).append(") total");
		int t=Integer.parseInt( this.CDQuery(total, null).get(0).toString());
		page.setTotal(t);
		//ʹ�ûص�����ʵ�ַ�ҳ
//		List<T> list=super.executeFind(new HibernateCallback() {
//			public Object doInHibernate(Session session) throws HibernateException,
//					SQLException {
//				Query query =session.createSQLQuery(sb.toString());
//				query.setResultTransformer(Transformers.aliasToBean(className));
//				query.setFirstResult(page.getiDisplayStart()); 
//				query.setMaxResults(page.getiDisplayLength());
//				System.out.println(page.getiDisplayStart()+"---------"+page.getiDisplayLength());
//				List<T> list=query.list();
//				return list;
//			}
//		});
		StringBuffer pageSql=new StringBuffer("select * from (select row_number() over (order by columnTemp) columnNum,* from (select top "+(page.getiDisplayStart()+page.getiDisplayLength())+" columnTemp=0 ,");
		pageSql.append(sb.toString().replaceFirst("select","") +") a ) aa where columnNum>"+page.getiDisplayStart()+"");
		List<T> list=this.CDQuery(pageSql, className);
		System.out.println(sb.toString());
		page.setResult(list) ;
		return page;
	}
}
